package com.example.teatea.dao;

import com.example.teatea.project.Good;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class GetGoodInMenu {
    public HashMap<Good,Integer> getGoodInMenu(int seat){
        int menu = 0;
        HashMap<Good,Integer> map = new HashMap<>();
        Connection connection = null;
        ResultSet resultSet = null;
        Properties info = new Properties();
        try {
            info.load(new FileInputStream("src\\main\\resources\\com\\example\\teatea\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            connection = DriverManager.getConnection(url, user, password);
            String sql1 = "select rMenu from seat where rId = ?";
            String sql2 = "select goodmenu.gName,gPrice,gNum from goods join goodmenu on goods.gName=goodmenu.gName where mId = ?";
            PreparedStatement presta = connection.prepareStatement(sql1);
            presta.setInt(1, seat);
            resultSet = presta.executeQuery();
            if(resultSet.next()){
                menu = resultSet.getInt("rMenu");
                System.out.println(menu);
            }
            presta = connection.prepareStatement(sql2);
            presta.setInt(1,menu);
            resultSet = presta.executeQuery();
            while (resultSet.next()) {
                Good good = new Good();
                good.setgName(resultSet.getString("gName"));
                good.setgPrice(resultSet.getFloat("gPrice"));
                map.put(good,resultSet.getInt("gNum"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return map;
    }
}
